扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:佚名 来源:中国自学编程网 2007年11月21日
关键字:
示例:
以下是引用片段: // this 关键字 // keywords_this.cs using System; class Employee { private string _name; private int _age; private string[] _arr = new string[5]; public Employee(string name, int age) { // 使用this限定字段,name与age this._name = name; this._age = age; } public string Name { get { return this._name; } } public int Age { get { return this._age; } } // 打印雇员资料 public void PrintEmployee() { // 将Employee对象作为参数传递到DoPrint方法 Print.DoPrint(this); } // 声明索引器 public string this[int param] { get { return _arr[param]; } set { _arr[param] = value; } } } class Print { public static void DoPrint(Employee e) { Console.WriteLine("Name: {0}\nAge: {1}", e.Name, e.Age); } } class TestApp { static void Main() { Employee E = new Employee("Hunts", 21); E[0] = "Scott"; E[1] = "Leigh"; E[4] = "Kiwis"; E.PrintEmployee(); for(int i=0; i<5; i++) { Console.WriteLine("Friends Name: {0}", E[i]); } Console.ReadLine(); } } /**//* |
Name: Hunts
Age: 21
Friends Name: Scott
Friends Name: Leigh
Friends Name:
Friends Name:
Friends Name: Kiwis
*/
注意点:
由于静态成员函数存在于类一级,并且不是对象的一部分,因此没有 this 指针。在静态方法中引用 this 是错误的。
索引器允许类或
婵″倹鐏夐幃銊╂姜鐢瓕鎻╅崚鍥╂畱閹厖绨$憴顤廡妫板棗鐓欓張鈧弬棰侀獓閸濅椒绗岄幎鈧張顖欎繆閹垽绱濋柇锝勭疄鐠併垽妲勯懛鎶姐€婄純鎴炲Η閺堫垶鍋栨禒璺虹殺閺勵垱鍋嶉惃鍕付娴f娊鈧柨绶炴稊瀣╃閵嗭拷